﻿using HalconDotNet;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
namespace AreaDetecVer2
{
    public partial class Form1 : Form
    {

        Halcon_AreaDetect h_areadetect = new Halcon_AreaDetect();
        bool IsPolycrystal = false;
        Stopwatch watch = new Stopwatch();
        public Form1()
        {
            InitializeComponent();
            this.button2.Enabled = false;
            //this.button5.Enabled = false;
            this.button4.Enabled = false;
            this.textBox1.ReadOnly = true;
            this.textBox2.ReadOnly = true;
            this.textBox3.ReadOnly = true;
            this.textBox4.ReadOnly = true;
            this.textBox5.ReadOnly = true;
            this.textBox6.ReadOnly = true;
            this.textBox7.ReadOnly = true;
            this.textBox8.ReadOnly = true;
            this.textBox9.ReadOnly = true;
            this.textBox10.ReadOnly = true;
            this.textBox11.ReadOnly = true;
            this.textBox12.ReadOnly = true;
            this.textBox13.ReadOnly = true;
            this.textBox14.ReadOnly = true;
            this.textBox15.ReadOnly = true;
            this.textBox16.ReadOnly = true;
            this.textBox17.ReadOnly = true;
            this.textBox18.ReadOnly = true;
            this.textBox19.ReadOnly = true;
            this.textBox26.ReadOnly = true;
            this.textBox27.ReadOnly = true;
            this.textBox28.ReadOnly = true;
            this.textBox29.ReadOnly = true;
            this.textBox34.ReadOnly = true;
         
        }

        public void button1_Click(object sender, EventArgs e)
        {
            
            try { 
                    string imgpath;
                     OpenFileDialog openFileDialog1 = new OpenFileDialog();
                     openFileDialog1.Filter = "图片文件(*.jpg,*.gif,*.bmp)|*.jpg|*.gif|*.bmp";
                    openFileDialog1.FilterIndex = 2;
                    openFileDialog1.RestoreDirectory = true;
           
                     if (openFileDialog1.ShowDialog() == DialogResult.OK)
                         {
                            imgpath = openFileDialog1.FileName;
                           h_areadetect.LoadImg(hWindowControl1.HalconWindow, imgpath);
                           this.button2.Enabled = true;
                          h_areadetect.DetecInit(hWindowControl1.HalconWindow);
                }
                   
            }
            catch (HalconException ex)
            {

                MessageBox.Show(ex.Message);
            }
            
        }

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                h_areadetect.LocateDetecRec();
                h_areadetect.WritepPrameter();
                this.button3.Enabled = true;
                this.button2.Enabled = false;        
            }
            catch (HalconException ex)
            {
                MessageBox.Show(ex.Message);
            }
        
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
               {
                watch.Start();
                TimeSpan time0 = watch.Elapsed;
                h_areadetect.ReadParameter();
                h_areadetect.GetLineEdgePt();
                h_areadetect.GetCornerPt();
                h_areadetect.GetConerAngle();
                h_areadetect.GetDiagonal();
                h_areadetect.GetArcEdgePt();
            if (comboBox1.Text == "单晶")
            {
                IsPolycrystal = false;
                h_areadetect.GetArcEdgePtToCricle();
                h_areadetect.CalMonocrystalParamater();
                h_areadetect.GetMonocrystalROI();
                h_areadetect.HoleDetect();    
                    watch.Stop();
                    TimeSpan time1 = watch.Elapsed;
                    TimeSpan time = time1 - time0;
                    double Totaltime = time.TotalMilliseconds;

                }
            else if (comboBox1.Text == "多晶")
            {
                IsPolycrystal = true;
                h_areadetect.GetArcEdgePtToLine();
                h_areadetect.GetPolycrystalROI();
                h_areadetect.HoleDetect();

                }
                else
            {
                MessageBox.Show("选择硅片类型");
                return;
            }
              //  h_areadetect.DisplayResult();//显示结果
               // this.button3.Enabled = false;
                this.button4.Enabled = true;
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("出现异常，请重试", ex.Message);
            }

        }

        private void button4_Click(object sender, EventArgs e)
        {
             try
              {
            if (!IsPolycrystal)
            {
               
                this.textBox15.Text = h_areadetect.ReturnArcRadius1();
                this.textBox16.Text = h_areadetect.ReturnArcRadius2();
                this.textBox17.Text = h_areadetect.ReturnArcRadius3();
                this.textBox18.Text = h_areadetect.ReturnArcRadius4();
                this.textBox19.Text = h_areadetect.ReturnCenterDiff();
              
                }
            else
            {
                h_areadetect.CalPolycrystalParamter();
                this.textBox15.Text = "NULL";
                this.textBox16.Text = "NULL";
                this.textBox17.Text = "NULL";
                this.textBox18.Text = "NULL";
                this.textBox19.Text = "NULL";
                
            }
                h_areadetect.HoleDetect();
                this.textBox1.Text = h_areadetect.ReturnEdge1Distance();
                this.textBox2.Text = h_areadetect.ReturnEdge2Distance();
                this.textBox3.Text = h_areadetect.ReturnArcLength1();
                this.textBox4.Text = h_areadetect.ReturnArcLength2();
                this.textBox5.Text = h_areadetect.ReturnArcLength3();
                this.textBox6.Text = h_areadetect.ReturnArcLength4();
                this.textBox7.Text = h_areadetect.ReturnEdgeAngle1();
                this.textBox8.Text = h_areadetect.ReturnEdgeAngle2();
                this.textBox9.Text = h_areadetect.ReturnEdgeAngle3();
                this.textBox10.Text = h_areadetect.ReturnEdgeAngle4();
                this.textBox11.Text = h_areadetect.ReturnDiagonalLength1();
                this.textBox12.Text = h_areadetect.ReturnDiagonalLength2();
                this.textBox13.Text = h_areadetect.ReturnDiagonalAngle();
                this.textBox14.Text = h_areadetect.ReturnHoleCount();
                this.textBox26.Text = h_areadetect.ReturnEdgeLengthU();
                this.textBox27.Text = h_areadetect.ReturnEdgeLengthR();
                this.textBox28.Text = h_areadetect.ReturnEdgeLengthD();
                this.textBox29.Text = h_areadetect.ReturnEdgeLengthL();
                h_areadetect.IsAreaDetectOK();
                this.textBox34.Text = h_areadetect.ReturnEdgeDetectResult();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("结果异常，请重试", ex.Message);
           }
        }
        private void button5_Click(object sender, EventArgs e)
        {
            double[] InputParameter = new double[10];
            InputParameter[0] = Convert.ToDouble(this.textBox20.Text);
            InputParameter[1] = Convert.ToDouble(this.textBox21.Text);
            InputParameter[2] = Convert.ToDouble(this.textBox22.Text);
            InputParameter[3] = Convert.ToDouble(this.textBox23.Text);
            InputParameter[4] = Convert.ToDouble(this.textBox24.Text);
            InputParameter[5] = Convert.ToDouble(this.textBox25.Text);
            InputParameter[6] = Convert.ToDouble(this.textBox30.Text);
            InputParameter[7] = Convert.ToDouble(this.textBox31.Text);
            InputParameter[8] = Convert.ToDouble(this.textBox32.Text);
            InputParameter[9] = Convert.ToDouble(this.textBox33.Text);
            h_areadetect.SetStardParameter(InputParameter);
        }
        private void button6_Click(object sender, EventArgs e)
        {
            Controls.OfType<TextBox>().ToList().ForEach(t => t.Clear());
        }

        private void button7_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void label21_Click(object sender, EventArgs e)
        {

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.comboBox1.SelectedIndex == 0)
            {
                this.textBox20.Text = "156.00";
                this.textBox21.Text = "156.00";
                this.textBox22.Text = "17.00";
                this.textBox23.Text = "90.00";
                this.textBox24.Text = "220.58";
                this.textBox25.Text = "90.00";
                this.textBox30.Text = "97.00";
                this.textBox31.Text = "0.5";
                this.textBox32.Text = "0.1";
                this.textBox33.Text = "0.1";
                 double[] InputParameter = new double[10];
            InputParameter[0] = Convert.ToDouble(this.textBox20.Text);
            InputParameter[1] = Convert.ToDouble(this.textBox21.Text);
            InputParameter[2] = Convert.ToDouble(this.textBox22.Text);
            InputParameter[3] = Convert.ToDouble(this.textBox23.Text);
            InputParameter[4] = Convert.ToDouble(this.textBox24.Text);
            InputParameter[5] = Convert.ToDouble(this.textBox25.Text);
            InputParameter[6] = Convert.ToDouble(this.textBox30.Text);
            InputParameter[7] = Convert.ToDouble(this.textBox31.Text);
            InputParameter[8] = Convert.ToDouble(this.textBox32.Text);
            InputParameter[9] = Convert.ToDouble(this.textBox33.Text);
            h_areadetect.SetStardParameter(InputParameter);
            }  
            if(this.comboBox1.SelectedIndex == 1)
            {
                this.textBox20.Text = "156.00";
                this.textBox21.Text = "156.00";
                this.textBox22.Text = "1.41";
                this.textBox23.Text = "90.00";
                this.textBox24.Text = "220.58";
                this.textBox25.Text = "90.00";
                this.textBox30.Text = "-1";
                this.textBox31.Text = "0.5";
                this.textBox32.Text = "0.1";
                this.textBox33.Text = "0.1";
                double[] InputParameter = new double[10];
                InputParameter[0] = Convert.ToDouble(this.textBox20.Text);
                InputParameter[1] = Convert.ToDouble(this.textBox21.Text);
                InputParameter[2] = Convert.ToDouble(this.textBox22.Text);
                InputParameter[3] = Convert.ToDouble(this.textBox23.Text);
                InputParameter[4] = Convert.ToDouble(this.textBox24.Text);
                InputParameter[5] = Convert.ToDouble(this.textBox25.Text);
                InputParameter[6] = Convert.ToDouble(this.textBox30.Text);
                InputParameter[7] = Convert.ToDouble(this.textBox31.Text);
                InputParameter[8] = Convert.ToDouble(this.textBox32.Text);
                InputParameter[9] = Convert.ToDouble(this.textBox33.Text);
                h_areadetect.SetStardParameter(InputParameter);
            }
                      
        }
    }
}
